package com.qf.controller;

import com.qf.pojo.User;
import com.qf.pojo.Userdenglu;
import com.qf.pojo.req.UserReq;
import com.qf.pojo.userbook;
import com.qf.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }

    @RequestMapping("/findByUid/{uid}")
    public User findByUid(@PathVariable("uid")Integer uid){
        return userService.findByUid(uid);
    }

    @RequestMapping("/login")
    public User login(@RequestBody Userdenglu userdenglu){
        String username=userdenglu.getUsername();
        String password=userdenglu.getPassword();
        User user = userService.selectuserbyusername(username);
        System.out.println(username);

        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        //认证shiro
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return user;
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return null;
        }

//        User user = userService.login(username, password);
//        if (user!=null){
//            return 1;
//        }else {
//            return 0;
//        }
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Integer registry(@RequestBody UserReq userReq){
        String email = userReq.getEmail();
        System.out.println(userReq);
        String o =(String)redisTemplate.opsForValue().get(email);
        if (o==null||!o.equals(userReq.getCode())){
            return 2;
        }
        //将数据添加到数据库
        String password = userReq.getPassword();
        String encode = new Md5Hash(password).toString();
        User user=new User(userReq.getUsername(),encode,userReq.getEmail());
        int register = userService.register(user);
        if (register>0){
            return 1;
        }else {
            return 0;
        }
    }
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public int addUser(@RequestBody User user){
        int add = userService.register(user);
        if (add>0){
            return 1;
        }else {
            return 0;
        }
    }
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public int updateUser(@RequestBody User user){
        int update = userService.update(user);
        if (update>0){
            return 1;
        }else {
            return 0;
        }
    }

    @RequestMapping("/deleteUser/{uid}")
    public int deleteUser(@PathVariable("uid")int uid) {
        int delete = userService.delete(uid);
        if (delete>0){
            return 1;
        }else {
            return 0;
        }
    }
    @RequestMapping("/tianjiashu")
    public int taijia(@RequestBody userbook userbook){
        System.out.println(userbook.getBid());
        int uid = userbook.getUid();
        int bid = userbook.getBid();
        System.out.println(uid+"------------------"+bid);
        int tiajiashu = userService.tiajiashu(userbook);
        return 1;
    }




}
